package com.zrrd.objectStream;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * @author WanJl
 * @version 1.0
 * @title ObjectOutputStreamDemo01
 * @description 对象序列化
 * @create 2024/5/26
 */
public class ObjectOutputStreamDemo01 {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            //创建基础流和处理流，指定要序列化的位置
            fos = new FileOutputStream("E:/users/序列化.txt");
            oos = new ObjectOutputStream(fos);

            //创建对象
            Student student = new Student("张三", 21);
            //将对象进行序列化
            oos.writeObject(student);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                oos.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
